package com.mycompany.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.realm.Realm;

public class MyRealm2 implements Realm{

	@Override
	public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token)
			throws AuthenticationException {
		String userName = (String)token.getPrincipal();
		String passWord = new String((char[])token.getCredentials());
		if(!"zhang".equals(userName)){
			throw new UnknownAccountException();
		}else if(!"123".equals(passWord)){
			throw new IncorrectCredentialsException();
		}
		return new SimpleAuthenticationInfo(userName,passWord,getName());
	}

	@Override
	public String getName() {
		return "MyRealm2";
	}

	@Override
	public boolean supports(AuthenticationToken token) {
		/*仅支持UsernamePasswordToken类型的Token*/
		return token instanceof UsernamePasswordToken;
	}
	
}
